<!-- 混凝土料仓管理 -->
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="show ? rowObj.T1 : rowObj.F1" :xs="show ? rowObj.T2 : rowObj.F2">
        <workSiteLeft ref="workSiteLeft" :show="show" @showLeft="showLeft" @queryVal="queryVal"/>
      </el-col>

      <el-col :span="show ? rowObj.T3 : rowObj.F3" :xs="show ? rowObj.T4 : rowObj.F4">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" style="display: flex;justify-content: space-between;">
          <div>
            <el-button style="margin: 3px 0 0 0;" size="mini" :icon="show ? 'el-icon-s-fold' : 'el-icon-s-unfold'" circle @click="showLeft(show)"></el-button>
            <el-form-item label="拌合站名称" prop="identifier" class="query">
              <el-select v-model="queryParams.identifier" clearable placeholder="请选择">
                <el-option v-for="item in stationList" :key="item.name" :label="item.name" :value="item.identifier"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="料仓名称" prop="materialStoreName">
              <el-input v-model="queryParams.materialStoreName" placeholder="料仓名称" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" v-hasPermi="['zhgd:hntSiloManage:query']">搜索</el-button>
              <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
            </el-form-item>
          </div>

          <el-form-item>
            <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['zhgd:hntSiloManage:add']">新增料仓</el-button>
          </el-form-item>
        </el-form>

        <!-- <el-row :gutter="10" class="mb8">
          <el-col :span="1.5"><el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['zhgd:hntSiloManage:add']">新增料仓</el-button></el-col>
          <el-col :span="1.5"><el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['zhgd:hntSiloManage:edit']">修改</el-button></el-col>
          <el-col :span="1.5"><el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['zhgd:hntSiloManage:remove']">删除</el-button></el-col>
          <el-col :span="1.5"><el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['zhgd:hntSiloManage:export']">导出</el-button></el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->

        <el-table border v-loading="loading" :data="asphaltSensorCorrectionList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" width="60" type="index"/>
          <el-table-column label="拌合机" align="center" prop="name" width="240"/>
          <el-table-column label="料仓编号" align="center" prop="materialStoreNumber"/>
          <el-table-column label="料仓类型" align="center" prop="materialStoreType"/>
          <el-table-column label="料仓名称" align="center" prop="materialStoreName"/>
          <el-table-column label="上限(%)" align="center" prop="upperLimit"/>
          <el-table-column label="下限(%)" align="center" prop="lowerLimit"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetails(scope.row)" v-hasPermi="['zhgd:hntSiloManage:details']">详情</el-button> -->
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['zhgd:hntSiloManage:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row, scope.$index)" v-hasPermi="['zhgd:hntSiloManage:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
      </el-col>
    </el-row>

    <!-- 添加或修改混凝土料仓管理对话框 -->
    <el-dialog :visible.sync="open" width="520px" :title="title" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="拌合机" prop="identifier">
          <el-select v-model="form.identifier" clearable placeholder="请选择" style="width: 100%;">
            <el-option v-for="item in stationList" :key="item.name" :label="item.name" :value="item.identifier"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="料仓编号" prop="materialStoreNumber"><el-input v-model="form.materialStoreNumber" placeholder="料仓编号"/></el-form-item>
        <el-form-item label="料仓名称" prop="materialStoreName"><el-input v-model="form.materialStoreName" placeholder="料仓名称"/></el-form-item>
        <el-form-item label="上限(%)" prop="materialStoreNumber"><el-input type="Number" v-model="form.upperLimit" placeholder="上限"/></el-form-item>
        <el-form-item label="下限(%)" prop="materialStoreName"><el-input type="Number" v-model="form.lowerLimit" placeholder="下限"/></el-form-item>
        <el-form-item label="料仓类型" prop="materialStoreType">
          <el-select v-model="form.materialStoreType" placeholder="料仓类型" clearable style="width: 100%;">
            <el-option v-for="dict in dict.type.material_store_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { hntSiloManageList, hntDetailsList, delHntSiloManageList, addHntSiloManageList, updateHntSiloManageList, hntSiloManageTypeList, deptTreeSelect } from "@/api/zhgd/hntSiloManage";
import { deptListData } from "@/api/zhgd/hntManage";
import workSiteLeft from "@/views/components/workSiteLeft";


export default {
  name: "HntSiloManage",
  dicts: ['sys_yes_no', 'material_store_type'],
  data() {
    return {
      show: true,
      rowObj: {
        T1: 3,
        T2: 24,
        T3: 21,
        T4: 24,
        F1: 0,
        F2: 0,
        F3: 24,
        F4: 24,
      },
      // 标段名称
      deptName: undefined,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 混凝土料仓管理表格数据
      asphaltSensorCorrectionList: [],
      // 拌合站搜索框数据
      stationList: [],
      // 料仓类型
      siloTypeData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationName: undefined,
        materialStoreName: undefined,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        // identifier: [{ required: true, message: "拌合机不能为空", trigger: "blur" }],
        // materialStoreType: [{ required: true, message: "料仓类型不能为空", trigger: "blur" }],
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  components: {
    workSiteLeft,
  },
  created() {
    this.getList();
    this.getSiloTypeList();
    this.getProjectList();
  },
  methods: {
    // 左侧树点击事件
    showLeft(show) {
      this.show = !show;
    },
    queryVal(val) {
      this.queryParams.id = val.id;
      this.handleQuery();
      this.getProjectList();
    },
    /** 查询混凝土料仓管理列表 */
    getList() {
      this.loading = true;
      hntSiloManageList(this.queryParams).then(response => {
        this.asphaltSensorCorrectionList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 查询拌合站列表 */
    getProjectList() {
      deptListData(this.queryParams).then((response) => {
        this.stationList = response;
      });
    },
    /** 查询料仓类型列表 */
    getSiloTypeList() {
      hntSiloManageTypeList().then((response) => {
        this.siloTypeData = response
      })
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.stationName = data.id;
      this.handleQuery();
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stationName: undefined,
        id: undefined,
        stationId: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.workSiteLeft.resetQuery();
      this.queryParams.id = "";
      this.resetForm("queryForm");
       this.queryParams.deptId = undefined;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加混凝土料仓管理";
    },
    /** 详情按钮操作 */
    // handleDetails(row) {
    //   this.loading = true;
    //   this.reset();
    //   const id = row.id || this.ids
    //   hntDetailsList(id).then(response => {
    //     this.loading = false;
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "混凝土料仓管理详情";
    //   });
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      hntDetailsList(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改混凝土料仓管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateHntSiloManageList(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addHntSiloManageList(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row, index) {
      const ids = row.id || this.ids;
      this.$modal.confirm(row.id ? `是否确认删除序号为"${index + 1}"的数据项？` : '是否确认删除选中数据项？').then(() => {
        this.loading = true;
        return delHntSiloManageList(ids);
      })
      .then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zhgd/asphaltSensorCorrection/export', {
        ...this.queryParams
      }, `asphaltSensorCorrection_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .query {
  .el-form-item__label {
    width: 90px !important;
  }
}
</style>
